package erp.server.message.service.impl;

import erp.server.base.exception.MessageServiceException;
import erp.server.base.response.StatusCode;
import erp.server.message.enums.MessageStatus;
import erp.server.message.mapper.*;
import erp.server.message.pojo.dto.MessageSaveDTO;
import erp.server.message.pojo.entity.*;
import erp.server.message.pojo.vo.DraftVO;
import erp.server.message.pojo.vo.MessageVO;
import erp.server.message.service.MessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    MessageMapper messageMapper;
    @Autowired
    MessageContentMapper messageContentMapper;
    @Autowired
    DraftsMapper draftsMapper;
    @Autowired
    OutboxMapper outboxMapper;
    @Autowired
    private InboxMapper inboxMapper;


    /**
     * 新建消息。新消息自动存放在草稿箱中。
     */
    @Override
    public void saveMessage(MessageSaveDTO messageSaveDTO) {
        // 新建需要的对象
        Message message = new Message();
        MessageContent messageContent = new MessageContent();
        Draft draft = new Draft();
        // 处理消息内容
        BeanUtils.copyProperties(messageSaveDTO, message);
        messageContent.setMessageId(message.getId());
        messageContent.setContent(messageSaveDTO.getContent());
        BeanUtils.copyProperties(messageSaveDTO, draft);
        draft.setMessageId(messageSaveDTO.getId());
        message.setUpdateTime(new Date());

        // 若消息id为空，则新建消息。
        if (messageSaveDTO.getId() == null) {
            message.setCreateTime(new Date());
            messageMapper.insert(message);
            messageContent.setMessageId(message.getId());
            messageContentMapper.insert(messageContent);
            draft.setMessageId(message.getId());
            draftsMapper.insert(draft);
        } else { // 更改消息
            // 检验该消息是否存在
            Message oldMessage = messageMapper.selectById(message.getId());
            Date createTime = new Date();

            if (oldMessage == null) { //若原消息已不存在，则新建一个消息
                message.setCreateTime(createTime);
                messageMapper.insert(message);
                messageContent.setMessageId(message.getId());
                messageContentMapper.insert(messageContent);
                draft.setMessageId(message.getId());
                draftsMapper.insert(draft);
            } else {
                createTime = oldMessage.getCreateTime();
                // 待完成 修改附件路径
                String attachmentPath = oldMessage.getAttachmentPath(); //获取原附件地址，准备删除数据
                BeanUtils.copyProperties(message, oldMessage);
                oldMessage.setCreateTime(createTime);
            }

            messageMapper.updateMessage(oldMessage);
            messageContentMapper.updateMessageContent(messageContent);
            // 更新草稿箱
            DraftVO oldDraft = draftsMapper.selectById(message.getId());
            if (oldDraft == null) { // 若草稿箱中无记录则新添
                draftsMapper.insert(draft);
            } else {
                draftsMapper.updateDraft(draft);
            }
        }
    }

    /**
     * 发送消息
     */
    @Override
    public void sendMessage(Long messageId) {
        /* 检查草稿箱中是否有对应消息
           复制消息
           将复制的消息填入到收件箱中

           修改消息状态;设置消息状态。
           将草稿箱中的消息移动到已发送信箱中
         */
        // 检查消息是否存在
        Message oldMessage = messageMapper.selectById(messageId);
        if (oldMessage == null) {
            throw new MessageServiceException(StatusCode.OPERATION_MESSAGE_NOT_FIND);
        }
        // 复制一份消息，发送到输出信箱
        Message newMessage = new Message();
        BeanUtils.copyProperties(oldMessage,newMessage);
        newMessage.setId(null);
        newMessage.setUpdateTime(new Date());
        newMessage.setCreateTime(new Date());
        newMessage.setSentTime(new Date());
        newMessage.setStatus(MessageStatus.FINISHED);
        // 复制消息内容
        MessageContent oldMessageContent = messageContentMapper.selectById(oldMessage.getId());
        if (oldMessageContent == null) { // 如果消息还在，而消息内容已经不存在。创新空的内容并告知用户。
            MessageContent messageContent = new MessageContent();
            messageContent.setMessageId(oldMessage.getId());
            messageContentMapper.insert(messageContent);
            throw new MessageServiceException(StatusCode.OPERATION_MESSAGE_NO_CONTENT);
        }
        messageMapper.insert(newMessage); // 确保有消息内容，可以发送再插入新消息。
        MessageContent newMessageContent = new MessageContent();
        newMessageContent.setMessageId(newMessage.getId());
        newMessageContent.setContent(oldMessageContent.getContent());
        messageContentMapper.insert(newMessageContent);
        // 将消息填入收件箱
        Inbox inbox = new Inbox();
        inbox.setMessageId(newMessage.getId());
        inbox.setUserId(newMessage.getDesUserId()); // 这里的id指所属收件方的id
        inbox.setOrgId(newMessage.getDesOrgId());
        inbox.setType(newMessage.getType());
        inboxMapper.insert(inbox);

        // 修改消息状态
        oldMessage.setUpdateTime(new Date());
        oldMessage.setSentTime(newMessage.getCreateTime());
        oldMessage.setStatus(MessageStatus.FINISHED);
        // 草稿箱中的消息移动到发件箱并设置失效时间。
        draftsMapper.deleteById(oldMessage.getId());
        Outbox outbox = new Outbox();
        outbox.setMessageId(oldMessage.getId());
        outbox.setUserId(oldMessage.getUserId());
        outbox.setOrgId(oldMessage.getOrgId());
        outbox.setType(oldMessage.getType());
        // 设置过期时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(oldMessage.getSentTime());
        calendar.add(Calendar.DAY_OF_MONTH,30);
        outbox.setOverdueTime(calendar.getTime());
        // 插入到已发送信箱
        outboxMapper.insert(outbox);
    }

    @Override
    public MessageVO selectMessageDetailById(Long messageId) {
        /*
        获取消息消息对象，获取消息内容对象
        组装成返回值对象并返回
         */
        MessageVO messageVO = new MessageVO();
        // 获取消息和内容对象
        Message message = messageMapper.selectById(messageId);
        MessageContent messageContent = messageContentMapper.selectById(messageId);
        // 组装
        BeanUtils.copyProperties(message,messageVO);
        BeanUtils.copyProperties(messageContent,messageVO);
        return messageVO;
    }
}















